package com.lyc.mall.service.impl;

import com.lyc.mall.dao.AdminDao;
import com.lyc.mall.model.PO.Admin;
import com.lyc.mall.model.BO.AdminAddBO;
import com.lyc.mall.model.BO.AdminLoginBO;
import com.lyc.mall.model.BO.AdminSearchBO;
import com.lyc.mall.model.VO.AdminInfoVO;
import com.lyc.mall.service.AdminService;
import com.lyc.mall.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    /**
     * 对应的sql语句是：select count(*) from admin where username = ? and password = ?;
     *
     * @param loginBO
     * @return
     */
    @Override
    public int loginBO(AdminLoginBO loginBO) {
        // 调用MyBatis相关代码
        // 每个线程都应该有它自己的sqlSession实例，sqlSession不能写成成员变量
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        Admin admin = new Admin(null, loginBO.getEmail(), loginBO.getPwd(), null);
        // MyBatis可以根据传进来的接口，在运行时动态地产生AdminDao的实现类
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        int count = adminDao.queryCount(admin);
        // sqlSession执行完后一定要记得提交并关闭
        sqlSession.commit();
        sqlSession.close();
        if (count == 0) {
            return 404;
        }
        return 200;
    }

    @Override
    public List<AdminInfoVO> allAdmins() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        List<AdminInfoVO> adminInfoVOS = adminDao.queryAll(new Admin());
        sqlSession.commit();
        sqlSession.close();
        return adminInfoVOS;
    }

    @Override
    public int addAdmin(AdminAddBO adminAddBO) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        Admin admin = new Admin(null, adminAddBO.getEmail(), adminAddBO.getPwd(), adminAddBO.getNickname());
        try {
            // 添加Admin
            adminDao.insertOne(admin);
            sqlSession.commit();
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return 404;
    }

    /**
     * 当前方法用于查询管理员详细信息
     *
     * @param adminID
     * @return
     */
    @Override
    public AdminInfoVO getAdminInfo(int adminID) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        AdminInfoVO adminInfoVO = adminDao.queryOne(adminID);
        sqlSession.commit();
        sqlSession.close();
        return adminInfoVO;
    }

    @Override
    public List<AdminInfoVO> getSearchAdmins(AdminSearchBO adminSearchBO) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        List<AdminInfoVO> adminInfoVOList = adminDao.queryAll(new Admin(null, adminSearchBO.getEmail(), null, adminSearchBO.getNickname()));
        sqlSession.commit();
        sqlSession.close();
        return adminInfoVOList;
    }
}
